define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'printmanager/org/index' + location.search,
                    add_url: 'printmanager/org/add',
                    edit_url: 'printmanager/org/edit',
                    del_url: 'printmanager/org/del',
                    multi_url: 'printmanager/org/multi',
                    check_url: 'printmanager/org/check',
                    table: 'self_printing_org',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [{
                            checkbox: true
                        },
                        {
                            field: 'id',
                            title: __('Id')
                        },
                        {
                            field: 'user_id',
                            title: __('User_id'),
                            searchable: false,
                            formatter: function(v,r,i){
                                return r.user_info.nickname
                            }
                        },
                        {
                            field: 'org_name',
                            title: __('Org_name')
                        },
                        {
                            field: 'org_code',
                            title: __('Org_code')
                        },
                        {
                            field: 'content',
                            title: __('合作内容'),
                            operate: "LIKE"
                        },
                        {
                            field: 'telephone',
                            title: __('Telephone')
                        },
                        {
                            field: 'linkname',
                            title: __('Linkname'),
                            searchable: false,
                        },
                        {
                            field: 'linktelephone',
                            title: __('Linktelephone')
                        },
                        {
                            field: 'address_detail',
                            title: __('地址'),
                            searchable: false,
                        },
                        {
                            field: 'qq',
                            title: __('Qq'),
                            searchable: false,
                        },
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {
                                "0": __('Status 0'),
                                "1": __('Status 1'),
                                "2": __('Status 2')
                            },
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.buttons,
                            buttons: [
                                {
                                    name: 'refuse',
                                    title: __('审核拒绝'),
                                    classname: 'btn btn-xs btn-primary btn-prompt',
                                    extend: ' data-prompt="2" data-datakey="reason" data-table-id="table" placeholder="请输入原因"',
                                    icon: 'fa fa-times',
                                    url: $.fn.bootstrapTable.defaults.extend.check_url + "?action=refuse",
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        Layer.alert(ret.msg);
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.status == 0;
                                    }
                                },
                                {
                                    name: 'check',
                                    title: __('审核通过'),
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    icon: 'fa fa-check',
                                    url: $.fn.bootstrapTable.defaults.extend.check_url  + "?action=check",
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        Layer.alert(ret.msg);
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.status == 0;
                                    }
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});